package com.hzncc.scaffold.controller.Rear.User;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.User;
import com.hzncc.scaffold.entity.Vo.LoginSuccessVo;
import com.hzncc.scaffold.entity.Vo.LoginVo;
import com.hzncc.scaffold.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @Author: DrunkBear
 * @Date: 2022-05-23 16:07
 * @Description: null
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "后台用户管理-登录")
public class LoginController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ApiOperation("登录")
    public ResultT login(@RequestBody LoginVo loginVo) {
        String account = loginVo.getAccount();
        String password = loginVo.getPassword();

        if (StrUtil.isEmpty(account) || StrUtil.isEmpty(password)) {
            return ResultT.fail("数据错误");
        }
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();

//        lqw.eq(User::getRole, "子管理员").and(userLambdaQueryWrapper -> userLambdaQueryWrapper.eq(StrUtil.isNotEmpty(account), User::getAccount, account)).and(userLambdaQueryWrapper -> userLambdaQueryWrapper.eq(User::getIsDelete, 0)).or().eq(User::getRole, "超级管理员").and(userLambdaQueryWrapper -> userLambdaQueryWrapper.eq(StrUtil.isNotEmpty(account), User::getAccount, account)).and(userLambdaQueryWrapper -> userLambdaQueryWrapper.eq(User::getIsDelete, 0));
        lqw.eq(User::getRole, "子管理员")
                .and(userLambdaQueryWrapper -> userLambdaQueryWrapper.eq(StrUtil.isNotEmpty(account), User::getAccount, account))
                .eq(User::getIsDelete, 0)
                .or()
                .eq(User::getRole, "超级管理员")
                .and(userLambdaQueryWrapper -> userLambdaQueryWrapper.eq(StrUtil.isNotEmpty(account), User::getAccount, account))
                .eq(User::getIsDelete, 0);
        if (userService.getOne(lqw) == null) {
            return ResultT.fail("账号密码错误");
        }
        User user = userService.getOne(lqw);
        String userPassword = user.getPassword();
        if (!user.getRole().equals("超级管理员") && !user.getRole().equals("子管理员")) {
            return ResultT.fail();
        }
        if (user.getStatus() == 1) {
            return ResultT.fail("账户已冻结,无法继续使用");
        }
        if (BCrypt.checkpw(password, userPassword)) {
            StpUtil.login(user.getId());
            String str = (String) StpUtil.getLoginId();
            System.out.println("str数据：" + str);


            LoginSuccessVo loginSuccessVo = BeanUtil.copyProperties(user, LoginSuccessVo.class, "routeAuth");

            ArrayList<String> routeAuth = new ArrayList<>();
            if (StrUtil.isNotEmpty(user.getRouteAuth())) {
                routeAuth = (ArrayList<String>) StrUtil.split(user.getRouteAuth(), ",");
            }

            loginSuccessVo.setUserId(user.getId());
            loginSuccessVo.setRouteAuth(routeAuth);
            loginSuccessVo.setToken(StpUtil.getTokenValue());

            return ResultT.success(loginSuccessVo);
        } else {
            return ResultT.fail("账号密码错误");
        }
    }


    @PostMapping("/logout")
    @ApiOperation("注销")
    public ResultT logout() {
        StpUtil.logout();
        return ResultT.success("注销成功");
    }
}
